package com.voice.call.utils;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtil {
	public static final String DATE_FMT="yyyy-MM-dd";
	public static final String DATE_HM_FMT="yyyy-MM-dd HH:mm";
	public static final String DATE_TIME_FMT="yyyy-MM-dd HH:mm:ss";
	public static final String DATE_NO_SPACE_FMT="yyyyMMdd";
	public static final String DATE_TIME_NO_SPACE_FMT="yyyyMMddHHmmss";
	/**
	 * 得到当前日期/时间字符串
	 * 
	 * @return 返回日期/时间字符串
	 */
	public static String getNowDateTimeString(String type) {
		Date date = new Date();
		SimpleDateFormat formattxt = new SimpleDateFormat(type);
		return formattxt.format(date);
	}
	
	public static String getTimeStringByDate(Date d, String type) {
        SimpleDateFormat formattxt = new SimpleDateFormat(type);
        return formattxt.format(d);
    }
	
	public static String getDateTimeString(Date date,String type) {
		SimpleDateFormat formattxt = new SimpleDateFormat(type);
		return formattxt.format(date);
	}
	
	public static Date parse(String dateStr, String fmt) {
        SimpleDateFormat sdf = new SimpleDateFormat(fmt);
        try {
            if (dateStr != null) {
                return sdf.parse(dateStr);
            }
        } catch (ParseException e) {
        }
        return new Date();
    }
	
	public static int getTodatRemainingTime() {
        String nowStr = getNowDateTimeString(DATE_TIME_FMT);
        String endStr = nowStr.substring(0, 10) + " 23:59:59";
        return DateUtil.getTimeDelta(nowStr, endStr);
    }
	
	public static int getTimeDelta(String dateStr1, String dateStr2) {
		Date date1 = parse(dateStr1,DATE_TIME_FMT);
		Date date2 = parse(dateStr2,DATE_TIME_FMT);
        long timeDelta = (date1.getTime() - date2.getTime()) / 1000;// 单位是秒
        int secondsDelta = timeDelta > 0 ? (int) timeDelta : (int) Math.abs(timeDelta);
        return secondsDelta;
    }
	
	public static Date addMonth(Date date, int index) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.MONTH, index);
		return cal.getTime();
	}
	
	
	public static Date addDate(Date date, int index) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.DATE, index);
		return cal.getTime();
	}
	
	public static Date addSecond(Date date, int index) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.SECOND, index);
		return cal.getTime();
	}
	
	public static Date addHour(Date date, int hour) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(Calendar.HOUR_OF_DAY, hour);
		return cal.getTime();
	}
	
	 /* 
     * 将时间戳转换为时间
     */
    public static String stampToDate(String s,String type){
        String res;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat(type);
        long lt = new Long(s);
        Date date = new Date(lt);
        res = simpleDateFormat.format(date);
        return res;
    }
    
   /***
    * 每周的第一天
    * @param date 时间
    * @param type 格式化类型
    * @return
    */
    public static Date getFirstOfWeekDay(Date date) {
        Calendar cal = Calendar.getInstance();
        cal.setTime(date);
        int d = 0;
        if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
            d = -6;
        } else {
            d = 2 - cal.get(Calendar.DAY_OF_WEEK);
        }
        cal.add(Calendar.DAY_OF_WEEK, d);
        // 所在周开始日期
       // String data1 = new SimpleDateFormat(type).format(cal.getTime());
        //return data1;
        return cal.getTime();
    }
    
    /***
     * 每周的最后一天
     * @param date 时间
     * @param type 格式化类型
     * @return
     */
     public static Date getLastOfWeekDay(Date date) {
         Calendar cal = Calendar.getInstance();
         cal.setTime(date);
         int d = 0;
         if (cal.get(Calendar.DAY_OF_WEEK) == 1) {
             d = -6;
         } else {
             d = 2 - cal.get(Calendar.DAY_OF_WEEK);
         }
         cal.add(Calendar.DAY_OF_WEEK, d+6);
         // 所在周结束日期
        // String data1 = new SimpleDateFormat(type).format(cal.getTime());
        // return data1;
         return cal.getTime();
     }
}
